package com.wangkm.roomdemo

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.TextView
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import java.util.*

/**
 * @author: created by wangkm
 * @Time: 2020/9/15 11:47
 * @Des： $
 * @Email: 1240413544@qq.com
 */
class MainActivity : AppCompatActivity() {
    private var mUserDao: UserDao? = null
    private var mMsgTV: TextView? = null
    private var mBuffer: StringBuffer? = null
    private val TAG = "thedata"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mUserDao = YxDatabase.getAppDatabase(applicationContext).userDao()
        mMsgTV = findViewById<View>(R.id.tv) as TextView
        mBuffer = StringBuffer()
    }

    fun onClick(view: View) {
        mBuffer!!.delete(0, mBuffer!!.length)
        when (view.id) {
            R.id.insert_one -> {
                doAsync {
                    //返回的是插入元素的primary key index
                    val aLong = mUserDao!!.insert(User("t" + System.currentTimeMillis() / 1000, "allen", 18))
                    if (aLong!! > 0) {
                        val msg = "insert one success, index is $aLong"
                        mBuffer!!.append(msg + "\n")
                        Log.i(TAG, msg)
                    } else {
                        val msg = "insert one fail "
                        mBuffer!!.append(msg + "\n")
                        Log.i(TAG, msg)
                    }
                    uiThread {
                        setMsg()
                    }
                }
            }
            R.id.insert_some -> {
                doAsync {
                    val users = ArrayList<User>()
                    users.add(User("t" + System.currentTimeMillis() / 1000, "jordan", 20))
                    users.add(User("t" + System.currentTimeMillis() / 1000, "james", 21))
                    val longs = mUserDao!!.insertAll(users)
                    if (longs != null && longs.size > 0) {
                        for (aLong in longs) {
                            val msg = "insert some success, index is $aLong"
                            mBuffer!!.append(msg + "\n")
                            Log.i(TAG, msg)
                        }
                    } else {
                        val msg = "insert some fail "
                        mBuffer!!.append(msg + "\n")
                        Log.i(TAG, msg)
                    }
                    uiThread {
                        setMsg()
                    }
                }
            }
            R.id.update_one -> {
                doAsync {
                    val random = Random().nextInt(9) + 1
                    val update = mUserDao!!.update(User(random, "t" + System.currentTimeMillis() / 1000, "kobe"))
                    if (update > 0) {
                        val msg = "update one success, index is $random"
                        mBuffer!!.append(msg + "\n")
                        Log.i(TAG, msg)
                    } else {
                        val msg = "update one fail ,index is $random the user item doesn't exist "
                        mBuffer!!.append(msg + "\n")
                        Log.i(TAG, msg)
                    }
                    uiThread {
                        setMsg()
                    }
                }
            }
            R.id.delete_one -> {
                doAsync {
                    val random = Random().nextInt(9) + 1
                    val delete = mUserDao!!.delete(User(random))
                    if (delete > 0) { //size 表示删除个数
                        val msg = "delete one  success,index is $random"
                        mBuffer!!.append(msg + "\n")
                        Log.i(TAG, msg)
                    } else {
                        val msg = "delete  one fail ,index is $random the user item doesn't exist "
                        mBuffer!!.append(msg + "\n")
                        Log.i(TAG, msg)
                    }
                    uiThread {
                        setMsg()
                    }
                }
            }
            R.id.find_one -> {
                doAsync {
                    val random = Random().nextInt(9) + 1
                    val user = mUserDao!!.findByUid(random)
                    if (user != null) {
                        val msg = "find one success , index is $random  user:  $user"
                        mBuffer!!.append(msg + "\n")
                        Log.i(TAG, msg)
                    } else {
                        val msg = "find one fail , index is $random the user item doesn't exist "
                        mBuffer!!.append(msg + "\n")
                        Log.i(TAG, msg)
                    }
                    uiThread {
                        setMsg()
                    }
                }
            }
            R.id.find_all -> {
                doAsync {
                    val all = mUserDao!!.all
                    if (all != null && all.size > 0) {
                        for (user1 in all) {
                            val msg = "find all success ,item  : $user1"
                            mBuffer!!.append(msg + "\n")
                            Log.i(TAG, msg)
                        }
                    } else {
                        val msg = "find all fail , no user item  exist "
                        mBuffer!!.append(msg + "\n")
                        Log.i(TAG, msg)
                    }
                    uiThread {
                        setMsg()
                    }
                }
            }
            R.id.delete_all -> {
                doAsync {
                    val all = mUserDao!!.all
                    if (all != null && all.size > 0) {
                        val i = mUserDao!!.deleteAll(all)
                        val msg = "delete all success , delete  size $i"
                        mBuffer!!.append(msg + "\n")
                        Log.i(TAG, msg)
                    } else {
                        val msg = "delete all fail , no user item  exist "
                        Log.i(TAG, msg)
                        mBuffer!!.append(msg + "\n")
                    }
                    uiThread {
                        setMsg()
                    }
                }
            }
        }
    }

    private fun setMsg() {
        val text = mBuffer.toString()
        mMsgTV!!.text = text
    }
}